package com.belief.aidaroe.utils;

import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 * 版权：(C) 版权所有 苏州彼立孚数据科技有限公司
 * <简述>
 * <详细描述>
 *
 * @author Lynkin
 * @create: 2021-07-19 09:54
 */
public class OperateRegistryUtils {

    /**
     * 把相应的值储存到变量中去 如果没有对应的节点，会自己创造一个对应的节点
     */
    public static void writeValue(String productparentnode,String productname,String keys ,String values) {
        // HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表值.
        Preferences pre = Preferences.userRoot().node("/"+productparentnode+"/"+productname);
        pre.put(keys, values);
    }
    public static void writeValue(String productparentnode,String keys ,String values) {
        // HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表值.
        // Preferences.userRoot(). 还有  Preferences.SystemRoot().的区别在我看来就是一个需要改注册表的读取权限，一个不用
        Preferences pre = Preferences.userRoot().node("/"+productparentnode);
        pre.put(keys, values);
    }

    /***
     * 根据key获取value
     *
     */
    public static String getValue(String productparentnode,String productname,String key) {
        Preferences pre = Preferences.userRoot().node("/"+productparentnode+"/"+productname);
        return pre.get(key, "");
    }

    public static String getValue(String productparentnode,String key) {
        Preferences pre = Preferences.userRoot().node("/"+productparentnode);
        return pre.get(key, "");
    }

    /***
     * 清除注册表
     *
     * @throws BackingStoreException
     */
    public static void clearValue(String productparentnode,String productname) throws BackingStoreException {
        Preferences pre = Preferences.userRoot().node("/"+productparentnode+"/"+productname);
        pre.clear();
    }

    /**
     * 谨慎使用，不然会删除该注册表目录的所有子目录结构
     * @param productparentnode
     * @throws BackingStoreException
     */
    public static void clearValue(String productparentnode) throws BackingStoreException {
        Preferences pre = Preferences.userRoot().node("/"+productparentnode);
        pre.clear();
    }

    public static void main(String[] args) {

        try {
            OperateRegistryUtils.writeValue("supermapSGS","iserver","setupVersion","9.1");
            OperateRegistryUtils.writeValue("supermapSGS","test","setupVersion","2.1");
            OperateRegistryUtils.writeValue("supermapSGS","iserver","setupLocal","E:\\");
            OperateRegistryUtils.writeValue("supermapSGS","test","setupLocal","E:\\");
            OperateRegistryUtils.writeValue("supermapSGS","datamanger","setupVersion","1");
            OperateRegistryUtils.writeValue("supermapSGS","datamanger","setupLocal","E:\\");


            //OperateRegistryUtils.writeValue("supermapSGS","datamanger","setupVersion","1.3");
            // OperateRegistryUtils.writeValue("supermapSGS","datamanger","setupLocal","E:\\CTCode");
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(OperateRegistryUtils.getValue("supermapSGS","datamanger","setupLocal"));
        try {
            // OperateRegistryUtils.clearValue("datamanger");
        } catch (Exception e) {
            e.printStackTrace();
        }

        //可以读取任意路径下的
       /* try {
            Process ps = null;
            ps = Runtime.getRuntime().exec("reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Prefs\\supermapSGS");
            ps.getOutputStream().close();
            InputStreamReader i = new InputStreamReader(ps.getInputStream());
            String line;
            BufferedReader ir = new BufferedReader(i);
            while ((line = ir.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }
}
